import React, { Component } from 'react';
import { StyleSheet, Text, View, Dimensions, Image, TouchableOpacity, TextInput } from "react-native";
import { offerStyle } from './offerStyle';
//import { Common } from '../../../component/common/Common.js';
import { Common } from '../../../../../component/common/Common';
import { style } from '../../../../../theme/style';
import Icon from 'react-native-vector-icons/AntDesign';
import ToastUtil from '../../../../../utils/toast';
import ConstData from '../../../../../utils/constData';
import navigationSeevice from '../../../../../components/navigation/navigationSeevice';


const ScreenWidth = Dimensions.get('window').width;
const scale = ScreenWidth / 375;

export class AddressCpt extends Component {
	constructor(props) {
		super(props);
		this.state = {
			addressList: this._startChange()
		}
		this.selectAdd = -1; // 点击哪个 赋值哪个
	}

	componentWillReceiveProps (props) {

	}

	//新增地址
	_addFn = () => {
		let { addressList } = this.state;
		addressList.push({ addrId: new Date().getTime().toString() });
		this.setState({
			addressList
		})
	}

	//
	_removeFn = (index) => {
		console.log('index', index)
		let { addressList } = this.state;
		let { callBack } = this.props || {};

		addressList.splice(index, 1);

		this.setState({
			addressList
		}, () => {
			callBack && callBack(this._changeFn());
		})
	}

	//
	_addressCallBack = (addData) => {
		if (!addData) return;
		let { callBack } = this.props || {};
		let { addrId, addressName } = addData[0] || {};
		let { addressList } = this.state;
		addressList[this.selectAdd].addrId = addrId;
		addressList[this.selectAdd].addressName = addressName;  //
		this.setState({
			addressList
		}, () => {
			callBack && callBack(this._changeFn());
		})

	}

	//帅选后数据转化
	_changeFn = () => {
		let { addressList } = this.state;
		let warehouseAddressIdArry = [];
		let warehouseAddressNameArry = [];
		console.log('addressList-090909', addressList)
		addressList && addressList.forEach((item, index) => {
			if (item.addrId != '' && item.addrId) {
				warehouseAddressIdArry.push(item.addrId);
				warehouseAddressNameArry.push(item.addressName);
			}
		})
		return { warehouseAddressNameArry, warehouseAddressIdArry };
	}

	//初始数据转化
	_startChange = () => {
		let { data } = this.props;
		let { eidtData } = data || {};
		let { warehouseAddressNameArry, warehouseAddressIdArry } = eidtData || {};
		let reselutArr = [];
		if (warehouseAddressNameArry && warehouseAddressNameArry.length && warehouseAddressIdArry && warehouseAddressIdArry.length) {
			warehouseAddressNameArry.forEach((item, index) => {
				reselutArr.push({ addrId: warehouseAddressIdArry[index], addressName: item })
			})
		} else {
			reselutArr.push({ addrId: '' });
		}
		return reselutArr;
	}

	render () {
		let { addressList } = this.state;
		return (
			<View style={styles.addressBox}>
				<View style={styles.addressTitle}>
					<Text style={offerStyle.fontSize}>仓库地址（交货地）</Text>
					<TouchableOpacity onPress={() => { this._addFn() }}>
						<Text style={[offerStyle.fontSize, styles.addressBtn]}>新增</Text>
					</TouchableOpacity>
				</View>
				<View style={styles.addressList}>
					{
						addressList && addressList.map((item, index) => {
							return (<TouchableOpacity style={styles.addressListItem} key={index} onPress={async () => {
								//获取企业id常量
								let Const = await ConstData.get();
								console.log('Const', Const)
								this.selectAdd = index;
								navigationSeevice.navigate('DeliveryAddress', {
									tCode: 'GP',//单选用
									isHiddenSwitch: true,
									companyId: Const.GLOBAL_SINOCHEM_COMPANY_ID, //GLOBAL_SINOCHEM_COMPANY_ID
									companyName: Const.GLOBAL_SINOCHEM_COMPANY_NAME, //
									getAddressCallBack: (addData) => {
										console.log('地址回调', addData);
										this._addressCallBack(addData)
									}
								})
							}}>
								<Text style={[styles.addressDetials, item.addressName ? { color: '#000' } : null, item.addressName ? offerStyle.fontBold : null]}>{item.addressName || "请选择仓库地址"}</Text>
								<Icon name="right" style={styles.addressRight} />
								{addressList.length > 1 ? <TouchableOpacity onPress={() => { this._removeFn(index) }}><Text style={styles.addressRemove}>删</Text></TouchableOpacity> : null}
							</TouchableOpacity>)
						})
					}
				</View>
			</View>
		)
	}
}

const styles = StyleSheet.create({
	addressBox: {
		//marginVertical: 15 * scale,
		marginTop: 15 * scale,
	},
	addressTitle: {
		flexDirection: 'row',
		justifyContent: 'space-between',
		marginBottom: 10 * scale
	},
	addressBtn: {
		color: style.color.themeColor
	},
	addressListItem: {
		flexDirection: 'row',
		alignItems: 'center',
		height: 38 * scale,
		backgroundColor: style.color.white,
		borderRadius: 6 * scale,
		marginBottom: 10 * scale,
		paddingHorizontal: 15 * scale
	},
	addressDetials: {
		width: 270 * scale,
		color: '#999',
		textAlignVertical: 'top'
	},
	addressRight: { fontWeight: 'bold', fontSize: 13 * scale },
	addressRemove: {
		color: 'red',
		fontSize: 13 * scale,
		paddingHorizontal: 10 * scale,
		paddingVertical: 10 * scale
	}
})